package com.oocl.jdbc_homework.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class FileIOUtil {

	public static boolean saveObject(String fileName , Object obj){
		
		ObjectOutputStream objectOut = null;
		try {
			objectOut = new ObjectOutputStream(new FileOutputStream(
					fileName));
			objectOut.writeObject(obj);
			objectOut.close();
			return true;
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				objectOut.close();
				return false;
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return false;
	}
	
	public static Object readObject(String fileName){
		Object obj = null;
		try{
			File file = new File(fileName);
			if(!file.exists())
				return null;
			ObjectInputStream objectIn = new ObjectInputStream(new FileInputStream(file));
			obj = objectIn.readObject();
		}catch(IOException e){
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			obj = null;
			e.printStackTrace();
		} finally{
			return obj;
		}
	}
	
	
}
